package repositorie

import (
	"github.com/kongyixueyuan.com/kongyixueyuan/web/initdatabase"
	"github.com/kongyixueyuan.com/kongyixueyuan/web/models"
	"go.mongodb.org/mongo-driver/mongo"
	"log"
)

type Userrepostorie struct {
}

func NewUserrepostorie() *Userrepostorie {
	return &Userrepostorie{}
}
func check(err error, funcName string) {
	if err != nil {
		log.Fatal("func ", funcName, "()  __ERROR__  :", err)
	}
}
func (u *Userrepostorie) Login(user string) models.Admin {
	var result models.Admin
	mg := initdatabase.NewMongo("Fabric", "Users")
	err := mg.FindOne("user", user).Decode(&result)
	check(err, "mg.FindOne")
	if result.User == "" {
		return models.Admin{}
	}
	return result
}

func (u *Userrepostorie) Register(user, passwrod string) *mongo.InsertOneResult {
	var result = models.Admin{
		User:     user,
		Password: passwrod,
	}
	mg := initdatabase.NewMongo("Fabric", "Users")
	return mg.InsertOne(result)
}
